package com.ymatou.iapi.pushbind.testcase;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.UnBindUserClientBean;
import com.ymatou.iapi.pushbind.service.UnBindUserClientCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PushCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
*解绑用户客户端
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnBindUserClient{
	private static UnBindUserClientBean unbinduserclientBean;
	private static UnBindUserClientCall unbinduserclientCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解绑用户客户端");
	}

	@Before
	public void caseUp() {
		unbinduserclientBean=new UnBindUserClientBean();
		unbinduserclientCall=new UnBindUserClientCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unbinduserclientCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindUserClient_001() {
		Logger.start(true,"解绑用户客户端,推手");
		try {
			int userId = new Random().nextInt(10000);
			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			PushCallService.bindUserClient(bindUserClientBean1);
			unbinduserclientBean.setAppId(bindUserClientBean1.getAppId());
			unbinduserclientBean.setAppIdType(bindUserClientBean1.getAppIdType());
			unbinduserclientBean.setClientId(bindUserClientBean1.getClientId());
			unbinduserclientBean.setDeviceId(bindUserClientBean1.getDeviceId());
			unbinduserclientBean.setDeviceToken("token" + YMTDateUtil.getDate());
			unbinduserclientBean.setUserId(bindUserClientBean1.getUserId());
			unbinduserclientBean.setPhoneType(bindUserClientBean1.getPhoneType());
			unbinduserclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			unbinduserclientCall.setData(unbinduserclientBean);
			unbinduserclientCall.callService();			
			Logger.verifyEquals("200",unbinduserclientCall.getString("Status"),"验证Status");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			List<Map> sqlMap = imDefaultWapper.selectPushBind(unbinduserclientBean.getClientId());
			Logger.verifyEquals(true,sqlMap.isEmpty(),"验证数据库为空");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getZrange(userId,"BindRecord:UserBindSet:2:"+userId);
			Logger.debug(set);
			Logger.verifyEquals(true,set.isEmpty(),"验证Redis中数据为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindUserClient_002() {
		Logger.start(true,"解绑用户客户端,解绑其中一台");
		try {
			int userId = new Random().nextInt(10000);

			String deviceId1 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-1);
			String deviceId2 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-3);
			String clientId1 = "Test" + YMTDateUtil.getBeforeOrNextSecond(-1);
			String clientId2 = "Test" + YMTDateUtil.getBeforeOrNextSecond(-3);
			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			Thread.sleep(3000);
			BindUserClientBean bindUserClientBean2 = PushCallService.intitUserClient(userId);
			bindUserClientBean1.setDeviceId(deviceId1);
			bindUserClientBean1.setClientId(clientId1);
			bindUserClientBean2.setDeviceId(deviceId2);
			bindUserClientBean2.setClientId(clientId2);
			PushCallService.bindUserClient(bindUserClientBean1);
			PushCallService.bindUserClient(bindUserClientBean2);
			unbinduserclientBean.setAppId(bindUserClientBean1.getAppId());
			unbinduserclientBean.setAppIdType(bindUserClientBean1.getAppIdType());
			unbinduserclientBean.setClientId(bindUserClientBean1.getClientId());
			unbinduserclientBean.setDeviceId(bindUserClientBean1.getDeviceId());
			unbinduserclientBean.setDeviceToken("token" + YMTDateUtil.getDate());
			unbinduserclientBean.setUserId(bindUserClientBean1.getUserId());
			unbinduserclientBean.setPhoneType(bindUserClientBean1.getPhoneType());
			unbinduserclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			unbinduserclientCall.setData(unbinduserclientBean);
			unbinduserclientCall.callService();
			Logger.verifyEquals("200",unbinduserclientCall.getString("Status"),"验证Status");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			List<Map> sqlMap = imDefaultWapper.selectPushBindBydeciveId(unbinduserclientBean.getDeviceId());
			List<Map> sqlMap1 = imDefaultWapper.selectPushBindBydeciveId(bindUserClientBean2.getDeviceId());
			Logger.verifyEquals(true,sqlMap.isEmpty(),"验证数据库为空");
			Logger.verifyEquals(false,sqlMap1.isEmpty(),"验证数据库不为null");

			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getZrange(userId,"BindRecord:UserBindSet:"+userId);
			Logger.debug(set);
			Logger.verifyEquals(false,set.isEmpty(),"验证Redis中数据为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindUserClient_003() {
		Logger.start(true,"解绑用户客户端,用户id为空，deviceId为空");
		try {
			int userId = new Random().nextInt(10000);
			String deviceId1 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-1);
			String clientId = "Test" + YMTDateUtil.getDate();
			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			PushCallService.bindUserClient(bindUserClientBean1);
			unbinduserclientBean.setAppId(bindUserClientBean1.getAppId());
			unbinduserclientBean.setAppIdType(bindUserClientBean1.getAppIdType());
			unbinduserclientBean.setClientId(bindUserClientBean1.getClientId());
		//	unbinduserclientBean.setDeviceId(bindUserClientBean1.getDeviceId());
			unbinduserclientBean.setDeviceToken("token" + YMTDateUtil.getDate());
			unbinduserclientBean.setPhoneType(bindUserClientBean1.getPhoneType());
			unbinduserclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			unbinduserclientCall.setData(unbinduserclientBean);
			unbinduserclientCall.callService();
			Logger.verifyEquals("406",unbinduserclientCall.getString("Status"),"验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}